package hn.cch.async.task;

import hn.cch.async.controller.AsyncController;
import hn.cch.async.service.AsyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnProperty(name = "scheduled.task.enable", havingValue = "true")// 通过配置设置开关
public class ScheduledTask {

    private static final Logger logger = LoggerFactory.getLogger(AsyncController.class);


    @Autowired
    private AsyncService asyncService;


    @Scheduled(cron = "${scheduled.task.sync.cron:*/1 * * * * ?}")
    public void sync(){
        logger.info("sync");
        asyncService.run(3);
        asyncService.async();
    }

    @Async
    @Scheduled(cron = "${scheduled.task.async.cron:*/1 * * * * ?}")
    public void async(){
        logger.info("async");
        asyncService.run(3);
        asyncService.async();

    }


}
